package com.mechat.dialog

import android.content.Context
import android.graphics.Color
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.view.MotionEvent
import android.view.View
import android.view.animation.Animation
import android.view.animation.AnimationUtils
import android.widget.ImageView
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import com.mechat.Constant
import com.mechat.R
import com.mechat.entity.BaseEntity
import com.mechat.entity.MatchResultEntity
import com.mechat.entity.MemberSubscribeEntity
import io.rong.imkit.event.EnumEventTag
import com.mechat.util.AnimatorUtils
import com.mechat.util.DensityUtil
import com.mechat.util.GlideUtil
import com.custom.base.config.BaseConfig
import com.custom.base.entity.OkHttpBodyEntity
import com.custom.base.http.OkHttpFromBoy
import com.custom.base.http.OkHttpManager
import com.custom.base.http.SDOkHttpResoutCallBack
import com.custom.base.util.IntentUtil
import com.google.firebase.messaging.RemoteMessage
import com.sunday.eventbus.SDEventManager
import io.rong.imkit.IMCenter
import io.rong.imkit.SpName
import io.rong.imkit.entity.OrderCreateEntity
import io.rong.imkit.event.FirebaseEventTag
import io.rong.imkit.http.HttpRequest
import io.rong.imkit.pay.EmptySuccessCallBack
import io.rong.imkit.utils.FirebaseEventUtils
import io.rong.imkit.utils.RouteUtils
import io.rong.imlib.IRongCallback
import io.rong.imlib.RongIMClient
import io.rong.imlib.model.Conversation
import io.rong.imlib.model.Message
import io.rong.message.TextMessage
import razerdp.basepopup.BasePopupWindow


/**
 * 消息推送弹窗
 */
class PushInsideDialog(var ctx: Context,var remoteMessage: RemoteMessage) :  BasePopupWindow(ctx) {
    init {
        setContentView(R.layout.dialog_push_inside)
        initView()

        val showAnim: Animation = AnimationUtils.loadAnimation(ctx, R.anim.anim_translation_down)
        val dismissAnim: Animation = AnimationUtils.loadAnimation(ctx, R.anim.anim_translation_up)
        showAnimation = showAnim
        dismissAnimation=dismissAnim

        Handler().postDelayed(Runnable {
            dismiss(true)
        },5000)
        setOutSideDismiss(false)
        isOutSideTouchable = true

    }
    private var mStartY: Int = 0
    private var mDiffY: Int = 0
    private fun initView() {


        val pushContainer = findViewById<ConstraintLayout>(R.id.push_container)
        val pushMatchContainer = findViewById<ConstraintLayout>(R.id.push_match_container)

        pushContainer.setOnTouchListener { _, event ->
            when (event.action) {
                MotionEvent.ACTION_DOWN -> {
                    mStartY = event.rawY.toInt()
                }
                MotionEvent.ACTION_MOVE -> {
                    mDiffY = event.rawY.toInt() - mStartY
                }
                MotionEvent.ACTION_UP -> {
                    if (kotlin.math.abs(mDiffY) < 100) {
                    } else {
                        dismiss()
                    }
                    mDiffY = 0

                }

                else -> {}
            }
            false
        }
        pushMatchContainer.setOnTouchListener { _, event ->
            event.run {
                when (action) {
                    MotionEvent.ACTION_DOWN -> {
                        mStartY = rawY.toInt()
                    }
                    MotionEvent.ACTION_MOVE -> {
                        mDiffY = rawY.toInt() - mStartY
                    }
                    MotionEvent.ACTION_UP -> {
                        if (kotlin.math.abs(mDiffY) < 100) {
                        } else {
                            dismiss()
                        }
                        mDiffY = 0

                    }

                    else -> {}
                }
            }
            false
        }


        val dialogAvatar = findViewById<ImageView>(R.id.dialog_avatar)
        val dialogSmallReply = findViewById<ImageView>(R.id.dialog_small_reply)
        val dialogTitle = findViewById<TextView>(R.id.dialog_title)
        val dialogContent = findViewById<TextView>(R.id.dialog_content)
        val dialogReply = findViewById<TextView>(R.id.txt_send_reply)
        val dialogAlbumsContainer = findViewById<ConstraintLayout>(R.id.dialog_albums_container)
        val dialogUserPhoto = findViewById<ImageView>(R.id.dialog_user_photo)
        val dialogPrivateLogo = findViewById<ImageView>(R.id.dialog_private_logo)
        val dialogVideoPlayLogo = findViewById<ImageView>(R.id.dialog_video_play_logo)
        val dialogVideoPlayPrivateLogo = findViewById<ImageView>(R.id.dialog_video_play_private_logo)
        val dialogPushLike = findViewById<ImageView>(R.id.dialog_push_like)

        val leftDialogAvatar = findViewById<ImageView>(R.id.dialog_left_avatar)
        val rightDialogAvatar = findViewById<ImageView>(R.id.dialog_right_avatar)
        val dialogMatchTitle = findViewById<TextView>(R.id.dialog_match_title)
        val dialogMatchContent = findViewById<TextView>(R.id.dialog_match_content)
        val dialogRightAvatarContent = findViewById<ConstraintLayout>(R.id.dialog_right_avatar_content)
        val dialogLeftAvatarContent = findViewById<ConstraintLayout>(R.id.dialog_left_avatar_content)
        val imgRedHeartSmall = findViewById<ImageView>(R.id.img_red_heart_small)
        val imgRedHeartBig = findViewById<ImageView>(R.id.img_red_heart_big)
        GlideUtil.setImageView(remoteMessage.notification?.imageUrl.toString(),dialogAvatar)
        dialogTitle.text=remoteMessage.notification?.title
        dialogContent.text=remoteMessage.notification?.body
        when(remoteMessage.data["type"]){
            "1"->{
                pushMatchContainer.visibility=View.GONE
                dialogSmallReply.visibility=View.GONE
                dialogReply.visibility=View.VISIBLE
                dialogAlbumsContainer.visibility=View.GONE
                dialogPushLike.visibility=View.GONE

            }
            "2"->{
                pushMatchContainer.visibility=View.GONE
                dialogSmallReply.visibility=View.VISIBLE
                dialogReply.visibility=View.GONE
                dialogAlbumsContainer.visibility=View.VISIBLE
                dialogUserPhoto.visibility=View.VISIBLE
                dialogPrivateLogo.visibility=View.GONE
                dialogVideoPlayLogo.visibility=View.GONE
                dialogVideoPlayPrivateLogo.visibility=View.GONE
                dialogPushLike.visibility=View.GONE

                GlideUtil.setImageView(remoteMessage.data["imageUrl"].toString(),dialogUserPhoto)
            }
            "3"->{
                pushMatchContainer.visibility=View.GONE
                dialogSmallReply.visibility=View.VISIBLE
                dialogReply.visibility=View.GONE
                dialogAlbumsContainer.visibility=View.VISIBLE
                dialogUserPhoto.visibility=View.VISIBLE
                dialogPrivateLogo.visibility=View.VISIBLE
                dialogVideoPlayLogo.visibility=View.GONE
                dialogVideoPlayPrivateLogo.visibility=View.GONE
                dialogPushLike.visibility=View.GONE

                GlideUtil.setImageView(remoteMessage.data["imageUrl"].toString(),dialogUserPhoto,true)
            }
            "4"->{
                pushMatchContainer.visibility=View.GONE
                dialogSmallReply.visibility=View.VISIBLE
                dialogReply.visibility=View.GONE
                dialogAlbumsContainer.visibility=View.VISIBLE
                dialogUserPhoto.visibility=View.VISIBLE
                dialogPrivateLogo.visibility=View.GONE
                dialogVideoPlayLogo.visibility=View.VISIBLE
                dialogVideoPlayPrivateLogo.visibility=View.GONE
                dialogPushLike.visibility=View.GONE

                GlideUtil.setImageView(remoteMessage.data["imageUrl"].toString(),dialogUserPhoto)
            }
            "5"->{
                pushMatchContainer.visibility=View.GONE
                dialogSmallReply.visibility=View.VISIBLE
                dialogReply.visibility=View.GONE
                dialogAlbumsContainer.visibility=View.VISIBLE
                dialogUserPhoto.visibility=View.VISIBLE
                dialogPrivateLogo.visibility=View.VISIBLE
                dialogVideoPlayLogo.visibility=View.GONE
                dialogVideoPlayPrivateLogo.visibility=View.VISIBLE
                dialogPushLike.visibility=View.GONE

                GlideUtil.setImageView(remoteMessage.data["imageUrl"].toString(),dialogUserPhoto,true)
            }
            "6"->{
                pushMatchContainer.visibility=View.GONE
                dialogSmallReply.visibility=View.GONE
                dialogReply.visibility=View.GONE
                dialogAlbumsContainer.visibility=View.GONE
                dialogPushLike.visibility=View.VISIBLE
            }
            "8"->{
                pushMatchContainer.visibility=View.VISIBLE
                pushContainer.visibility=View.GONE

                dialogMatchTitle.text=remoteMessage.notification?.title
                dialogMatchContent.text=remoteMessage.notification?.body
                GlideUtil.setImageView(remoteMessage.notification?.imageUrl.toString(),leftDialogAvatar)
                GlideUtil.setImageView(remoteMessage.data["imageUrl"].toString(),rightDialogAvatar)

                AnimatorUtils.playBounceAnimX(dialogLeftAvatarContent,0.0f, DensityUtil.dp2pxF(ctx,15f),500,200)
                AnimatorUtils.playBounceAnimX(dialogRightAvatarContent,DensityUtil.dp2pxF(ctx,72f), DensityUtil.dp2pxF(ctx,57f),500,200)

                Handler(Looper.getMainLooper()).postDelayed(Runnable {
                    imgRedHeartSmall.visibility=View.VISIBLE
                    imgRedHeartBig.visibility=View.VISIBLE
                },500)


                pushMatchContainer.setOnClickListener {
                }


            }
        }

        dialogPushLike.setOnClickListener {
            dismiss()
        }

        pushContainer.setOnClickListener {
            FirebaseEventUtils.logEvent(FirebaseEventTag.IM_Notification.name)
            if (remoteMessage.data["type"]!="6"){
                RouteUtils.routeToConversationActivity(
                    ctx,
                    Conversation.ConversationType.PRIVATE,
                    remoteMessage.data["targetId"].toString(),
                    true
                )
            }else{
            }

            dismiss()
        }
        setBackgroundColor(Color.TRANSPARENT)

    }
}